% Figure 8

% Delinquency and charge-off rates for securitized and nonsecuritized credit card receivables portfolios
% The upper panel plots the delinquency rates for securitized and
% nonsecuritized credit card receivables portfolios. The lower panel plots
% the charge-off rates for securitized and nonsecuritized credit card
% receivables portfolios. The delinquency and charge-off rate data are
% reported by the Federal Reserve Board.

clear; clc;

%load charge-off rate
load Data_Figure_08.mat

%load delinquency data
opts = spreadsheetImportOptions("NumVariables", 7);
opts.Sheet = "QuarterlyIndex";
opts.DataRange = "A4:G73";
opts.VariableNames = ["Date", "DelinqRateMastertrust", "USCreditCardDelinquencies30Days", "USCreditCardDelinquencies90Days", "FedDelinqRateAllBanksCreditCards", "DifferenceDelinqSecuritizedMinusFed", "FedChargeOffRateCreditCards"];
opts.VariableTypes = ["datetime", "double", "double", "double", "double", "double", "double"];
opts = setvaropts(opts, "Date", "InputFormat", "");
file = "Delinquencies.xlsx";
Delinquencies = readtable(file, opts, "UseExcel", false);
Delinquencies = table2timetable(Delinquencies,'RowTimes','Date');
idx = Delinquencies.Date<=datetime("31-Dec-2019");
Delinquencies = Delinquencies(idx,:);
clear opts


plotData = outerjoin(Delinquencies,MPRPortfYldExspreadChrgoff_q,key='Date',type='left');
plotData.FedChargeoffMinusMastertrustChargeoff = (plotData.FedChargeOffRateCreditCards - plotData.ChargeOffs1m)*100;
plotData.Date(end) = datetime("1-Jan-2020");
plotData.Year = year(plotData.Date);
plotData.Month= month(plotData.Date);



fig1 = figure('NumberTitle', 'Off', 'Name', 'Figure 1');

figOrientation = 'portrait';
figWidth = 8.0; 
figHeight = 6.0; 

set(fig1,'Units','inch');
set(fig1,'Position',[0 0 figWidth figHeight]);
movegui(fig1,'center')

set(fig1,'PaperUnits', 'centimeters');
set(fig1,'PaperSize', [figWidth figHeight]);
set(fig1,'PaperPosition',[0 0 figWidth figHeight]);
set(fig1,'PaperOrientation',figOrientation);
set(fig1,'InvertHardcopy','on');


subplot_er(2,1,1);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_1 = plot(datenum(plotData.Date),plotData.DelinqRateMastertrust);
set(plot_1, 'LineStyle', '-', 'Color', 'r' , 'LineWidth', 1.00);
hold on
plot_2 = plot(datenum(plotData.Date),plotData.FedDelinqRateAllBanksCreditCards);
set(plot_2, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(plotData.Date));
maxX = max(datenum(plotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylim([0 8])
ylabel('Percent')
title('Delinquency Rates','FontWeight','normal')
legend 'off';
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);





subplot_er(2,1,2);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_1 = plot(datenum(plotData.Date),plotData.ChargeOffs1m);
set(plot_1, 'LineStyle', '-', 'Color', 'r' , 'LineWidth', 1.00);
hold on
plot_2 = plot(datenum(plotData.Date),plotData.FedChargeOffRateCreditCards);
set(plot_2, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(plotData.Date));
maxX = max(datenum(plotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylim([0 12])
ylabel('Percent')
title('Charge-Off Rates','FontWeight','normal')
legend 'off';
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);



